﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace XeoEngine
{
    class SliderMenuEntry : MenuEntry
    {
        public int Value = 100;
        int minValue = 0, maxValue = 100;
        float selectionFade;
        Texture2D bt, bgt;
        int xpos;

        public event EventHandler<PlayerIndexEventArgs> Rolled;

        public SliderMenuEntry(string text, ContentManager content,int startvalue)
            : base(text)
        {
            bt = content.Load<Texture2D>("Menu/border");
            bgt = content.Load<Texture2D>("Menu/gradient");
            Value = startvalue;
        }

        public override void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
            if (isSelected)
            {
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
                HandleInput(screen.ScreenManager.Input, screen);
            }
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }

        public void HandleInput(InputState input,MenuScreen screen)
        {
            if (input.CurrentKeyboardStates[(int)PlayerIndex.One].IsKeyDown(Keys.Left))
            {
                if (Value >= minValue + 1)
                    Value--;
                if (Rolled != null)
                    Rolled(this, new PlayerIndexEventArgs(PlayerIndex.One));
            }
            if (input.CurrentKeyboardStates[(int)PlayerIndex.One].IsKeyDown(Keys.Right))
            {
                if (Value <= maxValue - 1)
                    Value++;
                if (Rolled != null)
                    Rolled(this, new PlayerIndexEventArgs(PlayerIndex.One));
            }
            MouseState ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Pressed)
                if (ms.Y > Position.Y - 15 && ms.Y < Position.Y + 15 && ms.X >= xpos && ms.X <= xpos + 500)
                {
                    Value = (ms.X - xpos) / 5;
                    if (Rolled != null)
                        Rolled(this, new PlayerIndexEventArgs(PlayerIndex.One));
                }
        }

        public override void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            Color color = isSelected ? Color.DeepSkyBlue : Color.LightGray;
            double time = gameTime.TotalGameTime.TotalSeconds;
            float pulsate = (float)Math.Sin(time * 6) + 1;
            float scale = 0.5f + pulsate * 0.05f * selectionFade;
            color *= screen.TransitionAlpha;
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;
            Vector2 origin = new Vector2(0, font.LineSpacing / 2);
            Vector2 fontsize = font.MeasureString(Text);
            xpos = (int)(Position.X + fontsize.X / 2) - 250;
            spriteBatch.Draw(bgt, new Rectangle(xpos, (int)Position.Y - 10, 5 * Value, 25), Color.White * screen.TransitionAlpha);
            spriteBatch.Draw(bt, new Rectangle(xpos + Value * 5 + 2, (int)Position.Y - 15, 35, 5), null, Color.White * screen.TransitionAlpha, MathHelper.PiOver2, Vector2.Zero, SpriteEffects.None, 0);
            spriteBatch.Draw(bt, new Rectangle(xpos - 5, (int)Position.Y - 15, 510, 5), Color.White * screen.TransitionAlpha);
            spriteBatch.Draw(bt, new Rectangle(xpos - 5, (int)Position.Y + 15, 510, 5), Color.White * screen.TransitionAlpha);
            spriteBatch.DrawString(font, Text, new Vector2(Position.X + fontsize.X/4, Position.Y), color, 0, origin, scale, SpriteEffects.None, 0);
        }
    }
}